﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AddIn.EA.Framework;
using AddIn.EA.Framework.Common;

namespace AddIn.EA.Architecture.ComponentLogic
{
    public class ProcessStructure
    {
        public ProcessStructure()
        {
            this.Processes = new List<ProcessObjects>();
            this.CreateCommunication = false;
            this.CreateResourceAccess = true;
        }

        public string GeneralName { get; set; }
        public string Module { get; set; }
        public bool CreateCommunication { get; set; }
        public bool CreateResourceAccess { get; set; }
        public List<ProcessObjects> Processes { get; set; }

        //public bool UpdateEA(IElement elementSelected, IElement elementReference) 

        public bool UpdateEA( Package model)
        {
            PackageBuilder structure = PackageBuilder.LoadProcessStructure(this.Module, this.GeneralName);

            BuildStructure(model, structure);

            model.Packages.Refresh();

            Package generalNamePackage = model.GetPackageByName(this.GeneralName, true);

            foreach (ProcessObjects process in this.Processes)
                if (!process.UpdateEA(generalNamePackage))
                    return false;

            return true;
        }

        private void BuildStructure(Package package, PackageBuilder structure)
        {
            if ((structure.Name == "Communication" && this.CreateCommunication) ||
                (structure.Name == "ResourceAccess" && this.CreateResourceAccess) ||
                "Communication|ResourceAccess".IndexOf(structure.Name) < 0)
            {
                Package childPackage = package.Packages.GetReferenceByName(structure.Name);
                if (childPackage == null)
                    childPackage = package.Packages.AddNew(structure.Name);

                childPackage.Update();

                foreach (PackageBuilder childStructure in structure)
                    BuildStructure(childPackage, childStructure);
            }
        }


    }
}
